package com.temi.demo.activity;

import com.temi.demo.BasePresenter;

/**
 * Activity基类
 * 注意：泛型P 要extends BasePresenter，限制类型，否则无法调用attachView方法
 */
public abstract class PresenterActivity<P extends BasePresenter, V> extends BaseActivity {

    private P presenter;

    @Override
    protected void initData() {
        if (presenter == null) {
            initPresenter();
        }
    }

    /**
     * Presenter的实例化，和绑定View，放到Base里面，具体还是由孩子实现
     */
    public void initPresenter() {
        presenter = createPresenter();
        if (presenter == null) {
            throw new NullPointerException("presenter空指针异常");
        }

        V view = createView();
        if (view == null) {
            throw new NullPointerException("view空指针异常");
        }
        presenter.attachView(view);
    }

    public abstract P createPresenter();

    public abstract V createView();

    public P getPresenter() {
        return presenter;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}
